package com.client.controller;

import com.Emplyment.dto.ClientUserDto;
import com.Emplyment.dto.ReUserPasswordDto;
import com.Emplyment.result.Result;
import com.Emplyment.vo.*;
import com.client.service.UserCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserCenterController {

    @Autowired
    private UserCenterService userCenterService;

    /**
     * 个人中心获取收藏公司
     */
    @GetMapping("/usercentercompany")
    public Result<List<ArticleVo>> getCollectCompanys(@RequestParam("username") String username) {
        log.info("用户名为：{}",username);
        List<ArticleVo> collectCompanys = userCenterService.getCollectCompanys(username);
        log.info("获取到的收藏公司：{}",collectCompanys);
        return Result.success(collectCompanys);
    }

    /**
     * 个人中心获取收藏的题目
     *
     */
    @GetMapping("/usercenterquestion")
    public Result<List<QuestionBankVo>> getCollectQuestions(@RequestParam("username") String username) {
        log.info("用户名为：{}",username);
        List<QuestionBankVo> collectQuestions = userCenterService.getCollectQuestions(username);
        log.info("获取到的收藏题目：{}",collectQuestions);
        return Result.success(collectQuestions);
    }

    /**
     * 取消个人收藏公司
     */
    @DeleteMapping("/canalusercompany")
    @Transactional
    public Result canalUserCompany(@RequestParam("username") String username, @RequestParam("companyId") Integer companyId) {
        log.info("用户名为：{} 公司id为：{}",username,companyId);
        userCenterService.canalUserCompany(username,companyId);
        return Result.success();
    }

    /**
     * 取消个人收藏题目
     */
    @DeleteMapping("/canaluserquestion")
    @Transactional
    public Result canalUserQuestion(@RequestParam("username") String username, @RequestParam("questionId") Integer questionId) {
        log.info("用户名为：{} 题目id为：{}", username, questionId);
        userCenterService.canalUserQuestion(username,questionId);
        return Result.success();
    }

    /**
     * 修改基本资料
     */
    @PutMapping("/update")
    public Result update(@RequestBody ClientUserDto clientUserDto) {
        log.info("用户名修改资料为：{}",clientUserDto);
        userCenterService.updateUser(clientUserDto);
        return Result.success();
    }

    /**
     * 修改头像
     */
    @PostMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam("username") String username , @RequestParam("avatarUrl") String avatarUrl) {
        userCenterService.updateAvatar(username,avatarUrl);
        log.info("头像修改为：{}",avatarUrl);
        return Result.success();
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetpassword")
    public Result resetPassword(@RequestBody ReUserPasswordDto reUserPasswordDto) {
        log.info("重置的数据为：{}",reUserPasswordDto);
        if(!reUserPasswordDto.getNewPassword().equals(reUserPasswordDto.getConfirmPassword())){
            return Result.error("两次密码输入不一致");
        }
        userCenterService.resetPassword(reUserPasswordDto);
        return Result.success();
    }


    /**
     * 获取用户发布文章
     */
    @GetMapping("/getarticle")
    public Result<List<InterViewVo>> getUserArticle(@RequestParam("username") String username) {
        log.info("获取用户发布文章的用户名为：{}", username);
        List<InterViewVo> interviewVos = userCenterService.getInterviewByUser(username);
        log.info("获取到的用户发布文章为：{}", interviewVos);
        return Result.success(interviewVos);
    }

    /**
     * 删除用户发布的讨论
     */
    @DeleteMapping("/deletearticle")
    @Transactional
    public Result deleteArticle(@RequestParam Integer interviewId) {
        log.info("删除的文章id为：{}", interviewId);
        userCenterService.deleteInterview(interviewId);
        return Result.success();
    }

    /**
     * 获取用户发布的评论
     */
    @GetMapping("/getcomment")
    public Result<List<UserComment>> getUserComments(@RequestParam("username") String username) {
        log.info("获取用户发布的评论的用户名为：{}", username);
        List<UserComment> commentVos = userCenterService.getCommentByUser(username);
        log.info("获取到的用户发布的评论为：{}", commentVos);
        return Result.success(commentVos);
    }

    /**
     * 删除用户发布评论
     */
    @DeleteMapping("/deletecomment")
    @Transactional
    public Result deleteComment(@RequestParam("username") String username,@RequestParam("interviewId") Integer interviewId,@RequestParam("Id") Integer Id) {
        log.info("删除用户:{}发布的评论id为：{}", username,Id);
        userCenterService.deleteComment(username,interviewId,Id);
        return Result.success();
    }

}
